$(function () {
   obj.init();
});
var obj = new function () {
    'use strict';
    return {
        dataGrid:null,
        type:null,
        typeId:null,
        init:function () {
            this.type = xhTool.getQuery('type');
            this.typeId = xhTool.getQuery('typeId');
            this.dataGrid = new xhDataGrid($('#list'),'ims/detail/getIn',{type:this.type, typeId:this.typeId});
            this.dataGrid.init().parseToolbar().activeButton(['bt_list_del','bt_list_edit'],[]);
            this.listener();
        },
        listener:function () {
            var $this = this;
            //出库
            $('#bt_list_add').click(function () {
                 $this.funObj().addForm();
            });

            //删除
            $('#bt_list_del').click(function () {
                if(xhExtra.buttonDisabled($(this))){
                    return false
                }
                $this.funObj().askDelAction();
            });

            $('#bt_list_edit').click(function () {
                if(xhExtra.buttonDisabled($(this))){
                    return false
                }
                $this.funObj().editForm();
            });
            
            
            //编辑价格
            // $('#bt_list_editPrice').click(function () {
            //     if(xhExtra.buttonDisabled($(this))){
            //         return false
            //     }
            //     $this.funObj().editPrice();
            // });

            //添加备注
            // $('#bt_list_addDesc').click(function () {
            //     if(xhExtra.buttonDisabled($(this))){
            //         return false
            //     }
            //     $this.funObj().addDesc();
            // });

            //搜索
            $('#bt_list_search').click(function () {
                $this.funObj().doSearch()
            });

            $('#bt_list_return').click(function () {
                $this.funObj().returnForm();
            })
        },
        funObj:function () {
            var $this = this;
            return {
                addForm:function (row) {
                    if (!row) {
                        window.top.xhTop.showModal({
                            title:'采购入库',
                            content: '<iframe src="'+baseUrl+'ims/detail/inForm"  style="width: 100%;;border-style: none;padding: 0px;height: 300px;"  scrolling="yes" ></iframe>',
                            button:'提交',
                            backClose: false
                        });
                    }
                },
                askDelAction:function () {
                    var ids = $this.dataGrid.getSelectValues('iPInId');
                    xhExtra.askChangeStatus('ims/detail/delIn',{ids:ids},$this.dataGrid,'确定要删除吗',function (res) {
                        window.top.xhTop.showSuccess(res.msg);
                    })
                },
                editPrice:function () {
                    var sProdName = $this.dataGrid.getSelectValue('iProdId');
                    var id = $this.dataGrid.getSelectValue('iPInId');
                    window.top.swal({
                        titleText: sProdName,
                        input: 'text',
                        inputPlaceholder: '价格',
                        inputAttributes: {class: 'form-control',id:'price'},
                        showCancelButton: true,
                        cancelButtonText: '取消',
                        confirmButtonText: '确认',
                        showLoaderOnConfirm: true,
                        onOpen:function () {
                            $('#price', parent.document).on('change',function () {
                                $this.funObj().toFormatDouble($(this).val(), $(this));
                            });
                        },
                        preConfirm: function (price) {
                            return new Promise(function (resolve, reject) {
                                if (!price || !/^([0-9]+)(\.\d{2})$/.test(price) || price <= 0) {
                                    reject('价格为必选项且必须为纯数字且大于0');
                                } else {
                                    resolve();
                                }
                            })
                        },
                        allowOutsideClick: true
                    }).then(function (price) {
                        xhExtra.ajaxRequest('ims/detail/modifyPrice',{id:id,price:price,type:'in'},null,function (res) {
                            window.top.xhTop.showSuccess(res.msg);
                            $this.dataGrid.reload();
                        });
                    }, function(dismiss) {});
                },
                addDesc:function () {
                    var id = $this.dataGrid.getSelectValue('iPInId');
                    window.top.swal({
                        titleText: '添加备注',
                        html:'<textarea type="date" class="form-control" id="mDesc" rows="3"></textarea>',
                        showCancelButton: true,
                        cancelButtonText: '取消',
                        confirmButtonText: '确认',
                        showLoaderOnConfirm: true,
                        preConfirm: function () {
                            return new Promise(function (resolve, reject) {
                                var desc = $('#mDesc' , window.top.document).val();
                                if (!desc) {
                                    reject('备注必填！');
                                } else {
                                    resolve(desc);
                                }
                            })
                        },
                        allowOutsideClick: true
                    }).then(function (desc) {
                        xhExtra.ajaxRequest('ims/detail/saveDesc',{id:id,sRemark:desc,type:'in'},null,function (res) {
                            window.top.xhTop.showSuccess(res.msg);
                            $this.dataGrid.reload();
                        });
                    }, function(dismiss) {});
                },
                toFormatDouble:function (number,dom) {
                    var f = parseFloat(number);
                    if (isNaN(f)) {
                        dom.val('0.00');
                    }
                    var f = Math.round(number*100)/100;
                    var s = f.toString();
                    var rs = s.indexOf('.');
                    if (rs < 0) {
                        rs = s.length;
                        s += '.';
                    }
                    while (s.length <= rs + 2) {
                        s += '0';
                    }
                    dom.val(s);
                },
                doSearch:function () {
                    $this.dataGrid.reload({
                        key:xhExtra.getValue($('#list_search')),
                        type:xhExtra.getValue($('#list_search_type')),
                        typeId:$this.typeId
                    })
                },
                returnForm:function () {
                    window.top.xhTop.showModal({
                        title:'退还商品',
                        content: '<iframe src="'+baseUrl+'ims/detail/returnForm"  style="width: 100%;;border-style: none;padding: 0px;height: 200px;"  scrolling="yes" ></iframe>',
                        button:'提交',
                        backClose: false
                    });
                },
                editForm:function () {
                    var ids = $this.dataGrid.getSelectValues('iPInId');
                    ids = ids.join(',');
                    window.top.xhTop.showModal({
                        title:'编辑',
                        content: '<iframe src="'+baseUrl+'ims/detail/modifyForm?type=in&ids='+ids+'"  style="width: 100%;;border-style: none;padding: 0px;height: 270px;"  scrolling="yes" ></iframe>',
                        button:'提交',
                        backClose: false,
                        width:650
                    });
                }
            }
        }
    }
};